package test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author Seon
 * @version 1.0
 * @since 2024-09-25
 */
public class SleepSort {

    private static final int[] ARRAY = {5, 3, 8, 4, 2};

    private static final ExecutorService EXECUTOR = Executors.newFixedThreadPool(ARRAY.length);

    public static void main(String[] args) throws InterruptedException {
        for (int i : ARRAY) {
            EXECUTOR.execute(() -> {
                try {
                    Thread.sleep(i * 100);
                    System.out.println(i);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
        }
        EXECUTOR.shutdown();
    }

//    public static void main(String[] args) {
//        Integer[] array = {5, 3, 8, 4, 2}; // 使用 Integer 类型以便与 List 兼容
//        List<Integer> list = new ArrayList<>(Arrays.asList(array));
//
//        System.out.println("Original array: " + list);
//        monkeySort(list);
//        System.out.println("Sorted array: " + list);
//    }
//
//    private static void monkeySort(List<Integer> list) {
//        while (!isSorted(list)) {
//            Collections.shuffle(list); // 使用 Collections.shuffle() 打乱列表
//            System.out.println("Shuffled: " + list);
//        }
//    }
//
//    private static boolean isSorted(List<Integer> list) {
//        for (int i = 1; i < list.size(); i++) {
//            if (list.get(i) < list.get(i - 1)) {
//                return false;
//            }
//        }
//        return true;
//    }

}